package centrecommande;

import api.Contract;
import api.EClientSocket;
import client.Mywraper;
import metier.Algo;
import snippet.tickerHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: keita
 * Date: 11/06/13
 * Time: 23:00
 * To change this template use File | Settings | File Templates.
 */
public class Sender extends EClientSocket implements Runnable {

    public final static int REQ_CONTRACT_DETAILS =2;
    int process= 0;
    //todo fichier de config pour port et IP
     static int nbClient=0 ;
    final static int port = 4001;  //4001 ou 7496
    final static String ip = "192.168.85.54";

    List <Contract> listeInstrument = new ArrayList<Contract>();

     public Sender(List <Contract> listeInstrument, int process) {
        super(Mywraper.getInstance());
        this.listeInstrument  =   listeInstrument;
        this.process = process;
        int tempnbClient = nbclient();
        System.out.println("Connect :"+this.eConnect(ip, port,tempnbClient));

    }

    synchronized private int nbclient() {
        return nbClient++;
    }


    @Override
    public void run() {
        int i=0;
        for (Contract contrat :listeInstrument) {
            i++;
            this.Call(nbClient*i,contrat);
        }


    }

    private void Call(int ID,Contract contrat) {
        if (this.isConnected())  {
            if (process==REQ_CONTRACT_DETAILS) this.reqContractDetails(ID, contrat);
            if (process==REQ_MKT_DATA) this.reqMktData(ID, contrat, "165", false);
        }



    }

}
